package com.kitty.web.common;


import com.kitty.web.domain.Sevenday;
import com.kitty.web.system.account.service.ITAccountServiceM;
import com.kitty.web.system.accountrole.service.ITRoleService;
import com.kitty.web.system.sevenday.service.ISevendayService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/common/echarts")
public class MainEcharts {
    private static Map roleMap = new HashMap();
    private static Map channelMap = new HashMap();
    private static List<Map> IpMap = new ArrayList<>();
    @Autowired
    private ITRoleService tRoleService;
    @Autowired
    private ITAccountServiceM itAccountService;
    @Autowired
    ISevendayService iSevendayService;
    @PostMapping("/queryRoles")
    @ResponseBody
    public AjaxResult queryRoles(){
        if(roleMap.size()>0){
            AjaxResult ajaxResult = new AjaxResult(AjaxResult.Type.SUCCESS,"",roleMap);
            return ajaxResult;
        }else{
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//设置格式
            List<Integer> onlineList = new ArrayList<>();
            List<Integer> resigerList = new ArrayList<>();
            List<String> keyList = new ArrayList<>();
            Sevenday sevenday = new Sevenday();
            for (int i = 0; i < 7; i++) {
                Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
                calendar.add(Calendar.DAY_OF_MONTH, -(7-i)); //当前时间减去一天，即一天前的时间
                System.out.println(simpleDateFormat.format(calendar.getTime()));
                sevenday.setCreatedate(simpleDateFormat.format(calendar.getTime()));
                List<Sevenday> sevendays = iSevendayService.selectSevendayList(sevenday);
                if(sevendays.size()>0){
                    onlineList.add(sevendays.get(0).getAmount().intValue());
                }else{
                    onlineList.add(0);
                }
                //Integer online= tRoleService.queryByOnline2(simpleDateFormat.format(calendar.getTime()))==null?0:tRoleService.queryByOnline2(simpleDateFormat.format(calendar.getTime()));
                Integer resiger = tRoleService.queryByResiger(simpleDateFormat.format(calendar.getTime()))==null?0:tRoleService.queryByResiger(simpleDateFormat.format(calendar.getTime()));

               // onlineList.add(online);
                resigerList.add(resiger);
                keyList.add(simpleDateFormat.format(calendar.getTime()));
            }
            Map map = new HashMap<>();
            map.put("online",onlineList);
            map.put("resiger",resigerList);
            map.put("keyList",keyList);
            roleMap = map;
            AjaxResult ajaxResult = new AjaxResult(AjaxResult.Type.SUCCESS,"",map);
            return ajaxResult;
        }
    }
    @PostMapping("/queryChannelRoles")
    @ResponseBody
    public AjaxResult queryChannelRoles(){
        if(channelMap.size()>0){
            AjaxResult ajaxResult = new AjaxResult(AjaxResult.Type.SUCCESS,"",channelMap);
            return ajaxResult;
        }else{
            List<Map<String, String>> maps = itAccountService.queryAccountByChannel();
            List<String> channel = new ArrayList<>();
            for (Map<String, String> map : maps) {
                String name = map.get("name");
                channel.add(name);
            }
            Map map = new HashMap<>();
            map.put("channel",channel);
            map.put("count",maps);
            channelMap = map;
            AjaxResult ajaxResult = new AjaxResult(AjaxResult.Type.SUCCESS,"",map);
            return ajaxResult;
        }
    }

    @PostMapping("/queryAccountIp")
    @ResponseBody
    public AjaxResult queryAccountIp(){
        if(IpMap.size()>0){
            AjaxResult ajaxResult = new AjaxResult(AjaxResult.Type.SUCCESS,"",IpMap);
            return ajaxResult;
        }else{

//            List<String> ips = new ArrayList<>();
//            List<String> strings = itAccountService.queryAccountIp();
//            for (String string : strings) {
//                if(StringUtils.isNotEmpty(string)){
//                    String addr =AddressUtils.getRealAddressByIP(string);
//                    ips.add(addr);
//                }
//            }
//            Map<String, Integer> map = new HashMap<>();
//            for(String a : ips){
//                //统计重复数的个数
//                map.put(a, map.get(a) == null? 1 : map.get(a)+1);
//            }
//            List<Map> resMap = new ArrayList<>();
//            for (String key : map.keySet()){
//                Map m = new HashMap();
//                m.put("name",key);
//                m.put("value",map.get(key));
//                resMap.add(m);
//            }
//            IpMap = resMap;
            AjaxResult ajaxResult = new AjaxResult(AjaxResult.Type.SUCCESS,"",IpMap);
            return ajaxResult;
        }
    }
    //@Scheduled(cron = "0 59 23 * * ?")
    public void saveOnlineRoles(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//设置格式
        Integer online= tRoleService.queryByOnline2(simpleDateFormat.format(new Date()))==null?0:tRoleService.queryByOnline2(simpleDateFormat.format(new Date()));
        Sevenday sevenday = new Sevenday();
        sevenday.setAmount(online.longValue());
        sevenday.setCreatedate(simpleDateFormat.format(new Date()));
        iSevendayService.insertSevenday(sevenday);

    }
    //@Scheduled(cron = "0 0 0 * * ?")
    public void queryEcharts(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//设置格式
        List<Integer> onlineList = new ArrayList<>();
        List<Integer> resigerList = new ArrayList<>();
        List<String> keyList = new ArrayList<>();
        Sevenday sevenday = new Sevenday();
        for (int i = 0; i < 7; i++) {
            Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
            calendar.add(Calendar.DAY_OF_MONTH, -(7-i)); //当前时间减去一天，即一天前的时间
            System.out.println(simpleDateFormat.format(calendar.getTime()));
            sevenday.setCreatedate(simpleDateFormat.format(calendar.getTime()));
            List<Sevenday> sevendays = iSevendayService.selectSevendayList(sevenday);
            if(sevendays.size()>0){
                onlineList.add(sevendays.get(0).getAmount().intValue());
            }else{
                onlineList.add(0);
            }
            //Integer online= tRoleService.queryByOnline2(simpleDateFormat.format(calendar.getTime()))==null?0:tRoleService.queryByOnline2(simpleDateFormat.format(calendar.getTime()));
            Integer resiger = tRoleService.queryByResiger(simpleDateFormat.format(calendar.getTime()))==null?0:tRoleService.queryByResiger(simpleDateFormat.format(calendar.getTime()));

            //onlineList.add(online);
            resigerList.add(resiger);
            keyList.add(simpleDateFormat.format(calendar.getTime()));
        }
        Map map = new HashMap<>();
        map.put("online",onlineList);
        map.put("resiger",resigerList);
        map.put("keyList",keyList);
        roleMap = map;

        List<Map<String, String>> maps = itAccountService.queryAccountByChannel();
        List<String> channel = new ArrayList<>();
        for (Map<String, String> map2 : maps) {
            String name = map2.get("name");
            channel.add(name);
        }
        Map map3 = new HashMap<>();
        map3.put("channel",channel);
        map3.put("count",maps);
        channelMap = map3;



        List<String> ips = new ArrayList<>();
        List<String> strings = itAccountService.queryAccountIp();
        for (String string : strings) {
            if(StringUtils.isNotEmpty(string)){
                String addr = AddressUtils.getRealAddressByIP(string);
                ips.add(addr);
            }
        }
        Map<String, Integer> map4 = new HashMap<>();
        for(String a : ips){
            //统计重复数的个数
            map4.put(a, map4.get(a) == null? 1 : map4.get(a)+1);
        }
        List<Map> resMap = new ArrayList<>();
        for (String key : map4.keySet()){
            Map m = new HashMap();
            m.put("name",key);
            m.put("value",map4.get(key));
            resMap.add(m);
        }
        IpMap = resMap;
    }
}
